//
//  HPContentView.m
//  hnGovernment
//
//  Created by 安仔 on 17/2/28.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "HPContentView.h"
#import "HPContentCell.h"
#import "ConvenientReusableView.h"
#import "SserviceByCategoryObj.h"

@interface HPContentView ()<UICollectionViewDelegate, UICollectionViewDataSource>
//微平台
@property(nonatomic, strong) UICollectionView *collectView;
//数据源
@property(nonatomic, strong) NSArray *collectDataArray;//内容列表数据
@property(nonatomic, strong) NSString *collectHeadStr;
@property(nonatomic, strong) NSArray *collectArray;//内容列表数据
@property(nonatomic, strong) NSDictionary *collectDataDict;//内容列表数据

@end

@implementation HPContentView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *touchView = [super hitTest:point withEvent:event];
    return touchView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        //[self setUpMyTableViewData];
        [self initCollectionView];
    }
    return self;
}

//初始化内容列表数据
- (void)setUpMyTableViewData {
    
    NSDictionary *dic1 = @{@"image":@"户籍办理s",
                           @"title":NSLocalString(@"户籍审批\n在线办理户口迁移、落户")};
    NSDictionary *dic2 = @{@"image":@"就业审批",
                           @"title":NSLocalString(@"就业审批\n校招、社招就业人员审批")};
    NSDictionary *dic3 = @{@"image":@"住房审批",
                           @"title":NSLocalString(@"住房审批\n住房公积金贷款，住房补贴审批")};
    NSDictionary *dic4 = @{@"image":@"更多服务",
                           @"title":NSLocalString(@"更多服务")};
    
    NSArray *oneSectionArr = @[dic1, dic2, dic3, dic4];
    
    self.collectDataDict = @{@"exhibitionName":@"线上审批",
                            @"exhibitionService":oneSectionArr};

}

//创建承载微应用的collectionView
- (void)initCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 400*m6Scale) collectionViewLayout:layout];
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    self.collectView.showsVerticalScrollIndicator = NO;
    self.collectView.showsHorizontalScrollIndicator = NO;
    self.collectView.scrollEnabled = YES;
    self.collectView.userInteractionEnabled = YES;
    self.collectView.backgroundColor = [UIColor whiteColor];
    //注册 colllectionCell
    [self.collectView registerClass:[HPContentCell class] forCellWithReuseIdentifier:@"HPContentCell"];
    [self.collectView registerClass:[ConvenientReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HPContentHeaderId"];
    [self addSubview:self.collectView];
}

#pragma mark- UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HPContentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HPContentCell" forIndexPath:indexPath];
    if (indexPath.row == 3) {
        [cell.titleLab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(cell.signImg.mas_centerY);
            make.left.mas_equalTo(cell.signImg.mas_right).offset(26*m6Scale);
        }];
        cell.titleLab.text = NSLocalString(@"更多服务");
        cell.detailsLab.hidden = YES;
        cell.signImg.image = [UIImage imageNamed:@"home_moreService"];
    } else {
        cell.detailsLab.hidden = NO;
        SserviceByCategoryObj *obj = self.collectDataArray[indexPath.row];
        if ([obj.intro isEqualToString:@""]) {
            [cell.titleLab mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(cell.signImg.mas_centerY);
                make.left.mas_equalTo(cell.signImg.mas_right).offset(26*m6Scale);
            }];
        } else {
            [cell.titleLab mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(cell.signImg.mas_centerY).offset(-10*m6Scale);
                make.left.mas_equalTo(cell.signImg.mas_right).offset(26*m6Scale);
            }];
        }
        cell.titleLab.text = obj.serviceName;
        cell.detailsLab.text = obj.intro;
        [cell.signImg sd_setImageWithURL:[NSURL URLWithString:obj.serviceImg]];
    }
    return cell;
}


//网格的头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        ConvenientReusableView *headerView = [self.collectView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HPContentHeaderId" forIndexPath:indexPath];
        
        if(headerView == nil) {
            headerView = [[ConvenientReusableView alloc] init];
            headerView.backgroundColor = [UIColor whiteColor];
        }
        [headerView.lineImg mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leftMargin.mas_equalTo(50*m6Scale);
            make.centerY.mas_equalTo(headerView.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(6*m6Scale, 32*m6Scale));
        }];
        headerView.lineImg.image = [UIImage imageNamed:@"bar"];
        headerView.titleLab.textColor = [UIColor blackColor];
        //显示默认header还是后台数据
        if (self.collectHeadStr.length == 0) {
            headerView.titleLab.text = NSLocalString(@"社保查询");
        } else {
            headerView.titleLab.text = self.collectHeadStr;
        }
        return headerView;
    }
    else if([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        return nil;
    }
    return nil;
}


#pragma mark- collectionView delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    [self.delegate collectionClick:indexPath.item];
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    CGSize size={KScreenWidth, 80*m6Scale};
    return size;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(KScreenWidth / 2, 142*m6Scale);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
}

//刷新数据
- (void) setData:(NSArray *)collectionArray headerString:(NSString *)headerStr {
    self.collectDataArray = collectionArray;
    self.collectHeadStr = headerStr;
    //NSInteger times1 = self.collectDataArray.count/2;
    //NSInteger times2 = self.collectDataArray.count%2;
    //CGRect rect = CGRectMake(0, 0, KScreenWidth, (times1+times2)*150*m6Scale + 80*m6Scale);
    //[self setCollectViewFrame:rect];
    [self.collectView reloadData];
}

- (void)setCollectViewFrame:(CGRect)frame {
    self.collectView.frame = frame;
    [self.collectView reloadData];
}

@end
